import json
import os
import re
import shutil
from os.path import exists

dirName = 'json'
filename = 'FreeDynamicWallpaper.json'

if __name__ == '__main__':
    txt = dirName + '/' + filename
    exist_file = exists(txt)
    if not exists(dirName):
        os.makedirs(dirName)
    empty = []

    videoDir = "/Users/victor/Program/Afra55/inc/FreeDynamicWallpaper4/video"
    videoList = os.listdir(videoDir)
    convert = lambda text: int(text) if text.isdigit() else text.lower()
    videoList.sort(key=lambda x: [convert(c) for c in re.split('([0-9]+)', x)])
    for file in videoList:
        if not file.startswith('.'):
            if file.endswith('.mp4'):
                file_ = str(file)[:-4]
                print(file_)
                name = file_
                video_url = "https://starrapps.github.io/FreeDynamicWallpaper4/video/" + name + ".mp4"
                preview_image_url = "https://starrapps.github.io/FreeDynamicWallpaper4/video/" + name + ".jpg"
                theme = {
                    'id': name,
                    'name': name,
                    'preview_image_url': preview_image_url,
                    'video_url': video_url,
                    'has_music': True
                }
                empty.append(theme)

    next_page = ''
    with open(txt, 'w', encoding='utf-8') as f:
            data_dict = {
                'themes': empty,
                'next_page': next_page
            }
            json_data = json.dumps(data_dict, indent=4)
            f.write(json_data)
            print(json_data)
            f.close()